package com.mystyle.le.weekproject.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.mystyle.le.weekproject.R;
import com.mystyle.le.weekproject.adapter.SeniorityFragmentAdapter;
import com.mystyle.le.weekproject.asynctaks.HeadListAsyncTaks;
import com.mystyle.le.weekproject.beans.Seniority;
import com.mystyle.le.weekproject.intentfactbackcall.MyCallBack;
import com.mystyle.le.weekproject.utils.ParseJson;
import com.mystyle.le.weekproject.utils.Url;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class DetialSortActivity extends AppCompatActivity {

    private ListView SeniorityFragment_LV;
    private Button btn_add;
    private String  ENCODE = null;
    private static final String PATH = "http://a121.baopiqi.com/api/mh/getSearchCartoon.php?&appname=%E7%88%B1%E6%83%85%E6%BC%AB%E7%94%BB%E7%B2%BE%E9%80%89&pkgname=com.platform.cartoonl&imei=863191020203140&versionname=1.2.7&limit=20&page=0&key=";
    private boolean isLast = false;
    private List<Seniority> seniorityLists = new ArrayList<>();
    private SeniorityFragmentAdapter seniorityFragmentAdapter;
    private int pageNum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_seniority);
        //初始化控件
        initView();
        //初始化数据
        initData();
        //通过传递过来的更多的值下载数据
        initChangeData();
        //ListView的点击跳转事件
        initChangeActivity();

    }

    /*
    * ListView的点击跳转事件
    * */
    private void initChangeActivity() {
        SeniorityFragment_LV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Seniority seniority = seniorityLists.get(position);
                String id1 = seniority.getId();
                Intent intent = new Intent(DetialSortActivity.this,DetialActivity.class);
                intent.putExtra("detialsoft_id",id1);
                startActivity(intent);
            }
        });
    }

    //通过传递过来的更多的值下载数据
    private void initChangeData() {
        Intent intent = getIntent();
        int addToady = intent.getIntExtra("addToady", 1);
       switch (addToady){
           case 1:
               downData(pageNum,1);
               initAddView(1);
               break;
           case 2:
               downData(pageNum,2);
               initAddView(2);
               break;
           case 3:
               downData(pageNum,3);
               initAddView(3);
               break;
       }
    }

    private void initAddView(final int type) {

         /*
        * 通过ListView的滑动事件判断是否到达底部
        * */
        SeniorityFragment_LV.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (isLast&&scrollState==SCROLL_STATE_IDLE){
                    btn_add.setVisibility(View.VISIBLE);
                }else {
                    btn_add.setVisibility(View.GONE);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                isLast = (firstVisibleItem+visibleItemCount)==totalItemCount;
            }
        });

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pageNum++;
                new HeadListAsyncTaks(new MyCallBack.HeadListBack() {
                    @Override
                    public void sendBack(String path) {
                        List<Seniority> seniorityList = ParseJson.parseSeniority(path);
                        if (seniorityList!=null){
                            seniorityLists.addAll(seniorityList);
                            SeniorityFragment_LV.setAdapter(seniorityFragmentAdapter);
                        }
                    }
                },DetialSortActivity.this).execute(Url.getRecommendMore(pageNum,type));
            }
        });
    }

    private void downData( int pageNum1, int type) {

        seniorityFragmentAdapter = new SeniorityFragmentAdapter(seniorityLists, DetialSortActivity.this);
       new HeadListAsyncTaks(new MyCallBack.HeadListBack() {
             @Override
             public void sendBack(String path) {
                 List<Seniority> seniorityList = ParseJson.parseSeniority(path);
                 if (seniorityList!=null){
                     seniorityLists.addAll(seniorityList);
                     SeniorityFragment_LV.setAdapter(seniorityFragmentAdapter);
                 }
             }
         },DetialSortActivity.this).execute(Url.getRecommendMore(pageNum1,type));

    }

    //获取传递过来的Name值
    private void initData() {
        try {
            Intent intent = getIntent();
            String boutiques_sort = intent.getStringExtra("boutiques_sort");
            String trim = intent.getStringExtra("trim");
            if (boutiques_sort!=null){
                ENCODE = URLEncoder.encode(boutiques_sort, "utf-8");
                getData(ENCODE);
            }
            if (trim!=null){
                ENCODE = URLEncoder.encode(trim, "utf-8");
                getData(ENCODE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
    * 通过传递过来的值，下载数据并展示在控件上
    * */
    private void getData(final String encode) {
        seniorityFragmentAdapter = new SeniorityFragmentAdapter(seniorityLists, DetialSortActivity.this);

        new HeadListAsyncTaks(new MyCallBack.HeadListBack() {
            @Override
            public void sendBack(String path) {
                List<Seniority> seniorityList = ParseJson.parseSeniority(path);
                if (seniorityList!=null){
                    seniorityLists.addAll(seniorityList);
                    SeniorityFragment_LV.setAdapter(seniorityFragmentAdapter);
                }
            }
        },DetialSortActivity.this).execute(PATH+encode);
    }

    /*
    * 初始化控件
    * */
    private void initView() {
        SeniorityFragment_LV = ((ListView) findViewById(R.id.SeniorityFragment_LV));
        btn_add = ((Button) findViewById(R.id.btn_add));
    }
}
